package com.example.payment.controller;

import com.example.common.resp.GlobalResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/discovery")
public class DiscoveryController {

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/info")
    public GlobalResult<String> discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("Service name: {}", service);

            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            for (ServiceInstance instance : instances) {
                log.info("ServiceId: {}, Host: {}, Port: {}, Uri: {}", instance.getServiceId(), instance.getHost(),
                        instance.getPort(), instance.getUri());
            }
        }

        return GlobalResult.success();
    }
}
